package com.aoip.telemetry.telemetry.configureui.business.analysis.access.service.impl;

import com.aoip.telemetry.telemetry.configureui.business.analysis.access.service.StationAccessService;
import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.equipment.StInfoConfigEntity;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.mapper.common.StInfoConfigMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2021/12/27 14:42
 */
@Service
@DS("telemetry")
public class StationAccessServiceImpl implements StationAccessService {

    @Resource
    private StInfoConfigMapper stInfoMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public List<StInfoConfigEntity> list(StInfoConfigEntity entity) {
        String adCode = entity.getAdCode();
        if (StringUtils.hasText(adCode)) {
            while (adCode.endsWith("00")) {
                adCode = adCode.substring(0, adCode.length() - 2);
            }
            entity.setAdCode(adCode);
        }
        return stInfoMapper.list(entity);
    }

    @Override
    public StInfoConfigEntity station(String id) {
        return stInfoMapper.selectById(id);
    }

    @Override
    public StInfoConfigEntity checkExist(String stcd, String stClassification) {
        QueryWrapper<StInfoConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("st_cd", stcd);
        queryWrapper.eq("st_classification", stClassification);
        List<StInfoConfigEntity> stInfoList = stInfoMapper.selectList(queryWrapper);
        if (stInfoList == null || stInfoList.size() < 1) {
            return null;
        }
        return stInfoList.get(0);
    }

    @Override
    public int insert(StInfoConfigEntity entity) {
        redisCache.setCacheObject(CacheKeyEnum.STATION + ":" + entity.getId() + entity.getStCd(), entity);
        return stInfoMapper.insert(entity);
    }

    @Override
    public int update(StInfoConfigEntity entity) {
        redisCache.setCacheObject(CacheKeyEnum.STATION + ":" + entity.getId() + entity.getStCd(), entity);
        return stInfoMapper.updateById(entity);
    }

    @Override
    public int initCache() {
        List<StInfoConfigEntity> list = list(new StInfoConfigEntity());
        Collection<String> keys = redisCache.keys(CacheKeyEnum.STATION + ":*");
        redisCache.deleteObject(keys);
        list.forEach(item -> redisCache.setCacheObject(CacheKeyEnum.STATION + ":" + item.getId() + item.getStCd(), item));
        return list.size();
    }

    @Override
    public void delete(String[] ids) {
        for (String id : ids) {
            Collection<String> keys = redisCache.keys(CacheKeyEnum.STATION + ":" + id + "*");
            redisCache.deleteObject(keys);
            stInfoMapper.deleteById(id);
        }
    }

}
